#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct part{
    char name[20];
    char password[20];
    }*a;

char n[20],n2[20];
char pass[20],pass2[20];
int time=1;
///RANDOM FUNCTION
void hash()
{
    int i;
    unsigned int pos,key=0,b,c;
    for(i=0;i<20;i++)
    {
        key=key + 5 * (int)(n[i]) + i;
    }
    key=key*key;
    b=key<<13;
    c=b>>26;
    pos=c;
    struct part temp;
    for(i=0;i<101;i++)
    {
        if(a[pos].name[0]=='\0')
        {
            strcpy(a[pos].name,n);        
            strcpy(a[pos].password,pass);
            break;
        }
        else
        {
            pos=pos+1;
        }
    }    
}

void check()
{
    unsigned int i,key=0,pos,b,c;
    int ch=0;
    for(i=0;i<20;i++)
    {
        key=key + 5 * n2[i] + i;
    }
    key=key*key;
    b=key<<13;
    c=b>>26;
    pos=c;
    for(i=0;i<101;i++)
    {    
        if(!(strcmp(a[pos].name,n2)))
        {
            ch=1;
            if(!(strcmp(a[pos].password,pass2)))
            {
                printf("Correct password \n");
                time=10;
                break;
            }
            else
            {
                printf("Incorrect password \n");
                time++;
                break;
            }
        }
        else
        {
        pos=pos+1;
        }
    }
    if(ch==0)
    {
    printf("Name not found\n");
    time++;
    }
}


void main()
{
    a = malloc(64*(sizeof(struct part)));
    FILE *fp;
    char line[63];
    fp=fopen("account.txt","r");
    int i;
    while(fgets(line, 126,fp))
        {
        sscanf(line,"%s %s\n",n,pass);
        hash();    
        }
    fclose(fp);
    while(1){
        if(time < 4){
        printf("Enter the name: ");
        scanf("%s",n2);
        printf("Enter the password: ");
        scanf("%s",pass2);
        check();}
        else if(time==10){
            break;}
        else{
            printf("Too many tries,forbidden access\n");
            break;
        }
    }
}